package com.example.android_common.util;

import com.example.android_common.view.iosdialog.ActionSheetDialog.OnSheetItemClickListener;
import com.example.android_common.view.iosdialog.AlertDialog;
import com.example.android_common.view.iosdialog.ProgressDialog;
import com.example.android_common.view.iosdialog.SheetDialogUtil;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View.OnClickListener;

public class BaseActivity extends FragmentActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppManager.getAppManager().addActivity(this);
	}
	
	@Override
	protected void onDestroy() {
		//页面销毁，取消本页所有请求
		//RequestManager.cancelAll(this);
		super.onDestroy();
	}
	
	/**
	 * 发起请求
	 * @param request
	 */
//	protected void executeRequest(Request<?> request) {
//		RequestManager.addRequest(request, this);
//	}
	
	/**
	 * 默认请求出错回调
	 * @return
	 */
//	protected Response.ErrorListener errorListener() {
//		return new Response.ErrorListener() {
//			@Override
//			public void onErrorResponse(VolleyError error) {
//				ShowUtils.showToast(R.string.request_fail_txt);
//			}
//		};
//	}

	/** ↓↓↓↓↓ 操作Fragment ↓↓↓↓↓ */
	protected void replaceFragment(int resId, Fragment fragment) {
		getSupportFragmentManager().beginTransaction().replace(resId, fragment).commit();
	}
	protected void replaceFragment(int resId, Fragment fragment, String tag) {
		getSupportFragmentManager().beginTransaction().replace(resId, fragment, tag).commit();
	}
	protected void addFragment(int resId, Fragment fragment) {
		getSupportFragmentManager().beginTransaction().add(resId, fragment).commit();
	}
	protected void showFragment(Fragment fragment) {
		getSupportFragmentManager().beginTransaction().show(fragment).commit();
	}
	protected void hideFragment(Fragment fragment) {
		getSupportFragmentManager().beginTransaction().hide(fragment).commit();
	}
	/** ↑↑↑↑ 操作Fragment ↑↑↑↑↑ **/
	
	/** ↓↓↓↓↓ 跳转操作 ↓↓↓↓↓ */
	private Intent redirect;
	/** Activity跳转 */
	protected void redirect(Class<?> cls){
		redirect = new Intent(this, cls);
		startActivity(redirect);
	}
	/** Activity跳转 */
	protected void redirect(Intent intent){
		startActivity(intent);
	}
	/** Activity跳转并关闭当前页 */
	protected void redirectClose(Class<?> cls){
		redirect = new Intent(this, cls);
		startActivity(redirect);
		AppManager.getAppManager().finishActivity(this);
	}
	/** Activity跳转并关闭当前页 */
	protected void redirectClose(Intent intent){
		startActivity(intent);
		AppManager.getAppManager().finishActivity(this);
	}
	/** 跳转页面并接收页面返回时传回的数据 */
	protected void redirectForResult(Class<?> cls,int requestCode){
		redirect = new Intent(this, cls);
		startActivityForResult(redirect,requestCode);
	}
	/** 跳转页面并接收页面返回时传回的数据 */
	protected void redirectForResult(Intent intent,int requestCode){
		startActivityForResult(intent,requestCode);
	}
	/** ↑↑↑↑↑跳转操作↑↑↑↑↑ */
	
	/** 显示Dialog窗口 */
	protected void showDialog(String message,OnClickListener positive,OnClickListener negative){
		getDialog(null, message, "", "", positive, negative).show();
	}
	protected void showDialog(String title,String message,OnClickListener positive,OnClickListener negative){
		getDialog(title, message, "", "", positive, negative).show();
	}
	protected AlertDialog getDialog(String message,OnClickListener positive,OnClickListener negative){
		return getDialog(null, message, "", "", positive, negative);
	}
	protected AlertDialog getDialog(String title,String message,OnClickListener positive,OnClickListener negative){
		return getDialog(title, message, "", "", positive, negative);
	}
	private AlertDialog getDialog(String title,String message,
			String button1,String button2,
			OnClickListener positive,OnClickListener negative){
		AlertDialog dialog = new AlertDialog(this).builder();
		if(!TextUtils.isEmpty(title))
			dialog.setTitle(title);
		dialog.setMsg(message);
		dialog.setPositiveButton(button1, positive);
		dialog.setNegativeButton(button2, negative);
		return dialog;
	}
	
	/**
	 * 显示ActionSheetDialog
	 * @param names
	 * @param listener
	 */
	protected void showSheetDialog(String[] names,OnSheetItemClickListener listener){
		SheetDialogUtil.showActionSheetDialog(this, names, listener);
	}
	
	/** ↓↓↓↓↓ 显示ProgressDialog ↓↓↓↓↓ */
	protected ProgressDialog progressDialog;
	protected void showProgressDialog(){
		progressDialog = new ProgressDialog(this).builder();
		progressDialog.show();
	}
	protected void hideProgressDialog(){
		if(progressDialog!=null && progressDialog.isShowing()){
			progressDialog.dismiss();
		}
	}
	/** ↑↑↑↑↑ 显示ProgressDialog ↑↑↑↑↑ */
}